{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convert to and from Pandas DataFrames\n",
    "====================\n",
    "There are no built in methods for the conversions but these functions below should work in most basic instances."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# remove comment to use latest development version\n",
    "import sys; sys.path.insert(0, '../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import raccoon as rc\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Raccoon to Pandas\n",
    "----------------\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rc_to_pd(raccoon_dataframe):\n",
    "    \"\"\"\n",
    "    Convert a raccoon dataframe to pandas dataframe\n",
    "\n",
    "    :param raccoon_dataframe: raccoon DataFrame\n",
    "    :return: pandas DataFrame\n",
    "    \"\"\"\n",
    "    data_dict = raccoon_dataframe.to_dict(index=False)\n",
    "    return pd.DataFrame(data_dict, columns=raccoon_dataframe.columns, index=raccoon_dataframe.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'raccoon.dataframe.DataFrame'>\n",
      "  index    a    b\n",
      "-------  ---  ---\n",
      "      7    1    4\n",
      "      8    2    5\n",
      "      9    3    6\n"
     ]
    }
   ],
   "source": [
    "rc_df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[7, 8, 9])\n",
    "print(type(rc_df))\n",
    "print(rc_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "   a  b\n",
      "7  1  4\n",
      "8  2  5\n",
      "9  3  6\n"
     ]
    }
   ],
   "source": [
    "pd_df = rc_to_pd(rc_df)\n",
    "print(type(pd_df))\n",
    "print(pd_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas to Raccoon\n",
    "-----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pd_to_rc(pandas_dataframe):\n",
    "    \"\"\"\n",
    "    Convert a pandas dataframe to raccoon dataframe\n",
    "\n",
    "    :param pandas_dataframe: pandas DataFrame\n",
    "    :return: raccoon DataFrame\n",
    "    \"\"\"\n",
    "\n",
    "    columns = pandas_dataframe.columns.tolist()\n",
    "    data = dict()\n",
    "    pandas_data = pandas_dataframe.values.T.tolist()\n",
    "    for i in range(len(columns)):\n",
    "        data[columns[i]] = pandas_data[i]\n",
    "    index = pandas_dataframe.index.tolist()\n",
    "    index_name = pandas_dataframe.index.name\n",
    "    index_name = 'index' if not index_name else index_name\n",
    "    return rc.DataFrame(data=data, columns=columns, index=index, index_name=index_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "   a  b\n",
      "5  1  4\n",
      "6  2  5\n",
      "7  3  6\n"
     ]
    }
   ],
   "source": [
    "pd_df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=[5, 6, 7], columns=['a', 'b'])\n",
    "print(type(pd_df))\n",
    "print(pd_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'raccoon.dataframe.DataFrame'>\n",
      "  index    a    b\n",
      "-------  ---  ---\n",
      "      5    1    4\n",
      "      6    2    5\n",
      "      7    3    6\n"
     ]
    }
   ],
   "source": [
    "rc_df = pd_to_rc(pd_df)\n",
    "print(type(rc_df))\n",
    "print(rc_df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:.conda-raccoon]",
   "language": "python",
   "name": "conda-env-.conda-raccoon-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
